package com.foo.TextPreProcessing;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import weka.core.Attribute;
import weka.core.FastVector;
import weka.core.Instance;
import weka.core.Instances;


public class CreatinganArffFile 
{

	public List<DataBean> articles;
	public Instances createDataset() throws Exception {

		FastVector atts = new FastVector(4);
		atts.addElement(new Attribute("source", (FastVector) null));
		atts.addElement(new Attribute("title", (FastVector) null));
		atts.addElement(new Attribute("description", (FastVector) null));
		atts.addElement(new Attribute("category", (FastVector) null));
		Instances data = new Instances("News Classification", atts, 0);
		
		try {
			articles = readXMLFile();
			System.out.println("number of articles " + articles.size());
			if(articles.size() > 0)
			{
				for(int i=0;i<articles.size();i++)
				{
					double[] newInst = new double[4];
					
					newInst[0] =(double)data.attribute("source").addStringValue(articles.get(i).getSource());
					newInst[1] =(double)data.attribute("title").addStringValue(articles.get(i).getTitle());
					newInst[2] =(double)data.attribute("description").addStringValue(articles.get(i).getDesciption());
					newInst[3] =(double)data.attribute("category").addStringValue(articles.get(i).getCategory());
					
					data.add(new Instance(1.0, newInst));
				}
				
				System.out.println("Arff Dataset is: " + data);
			}
			
	    } 
		catch (Exception e) {
			//System.err.println("failed to convert file: " + directoryPath + File.separator + files[i]);
		}
		return data;
	}

	public List<DataBean> readXMLFile()
	{
		File newsData = new File("/tmp/newsspace200.xml");
		//File newsData = new File("testdata/news_small.xml");
		//File newsData = new File("Arff File/newsData.xml");
		List<DataBean> articles = null;
		
		try
		{
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(newsData);
			doc.getDocumentElement().normalize();
			NodeList nodes = doc.getElementsByTagName("all_news");
						
			//NodeList nodes = doc.getChildNodes();
			for (int i = 0; i < nodes.getLength(); i++) 	
			{
				Node node = nodes.item(i);

				if (node.getNodeType() == Node.ELEMENT_NODE) 
				{
					Element element = (Element) node;
					int datasetCount = element.getElementsByTagName("source").getLength();
					System.out.println("Found " + datasetCount + " elements in XML file");
					articles = new ArrayList<DataBean>(datasetCount);
					
					for (int d=0;d<datasetCount;d++)
					{
						System.out.println("Processing article# " + d);
						DataBean newdata= new DataBean();
						if (element.getElementsByTagName("source").item(d).getChildNodes().item(0) != null) {
						    newdata.setSource(element.getElementsByTagName("source").item(d).getChildNodes().item(0).getNodeValue());
						}
						if (element.getElementsByTagName("description").item(d).getChildNodes().item(0) != null) {
							newdata.setDesciption(element.getElementsByTagName("description").item(d).getChildNodes().item(0).getNodeValue());							
						}
						if (element.getElementsByTagName("title").item(d).getChildNodes().item(0) != null) {
							newdata.setTitle(element.getElementsByTagName("title").item(d).getChildNodes().item(0).getNodeValue());
						}
						if (element.getElementsByTagName("category").item(d).getChildNodes().item(0) != null) {
						    newdata.setCategory(element.getElementsByTagName("category").item(d).getChildNodes().item(0).getNodeValue());
						}
						articles.add(newdata);
					}					
				}
			}	
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return articles;
	}


	public static void main( String[] args )
	{
		CreatinganArffFile obj = new CreatinganArffFile();
		try {
			obj.createDataset();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
